package com.partybuilding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.partybuilding.annotation.DataScope;
import com.partybuilding.constants.SystemConstants;
import com.partybuilding.domain.ResponseResult;
import com.partybuilding.domain.entity.Partytable;
import com.partybuilding.domain.entity.Usertable;
import com.partybuilding.domain.vo.UserCheckVo;
import com.partybuilding.enums.AppHttpCodeEnum;
import com.partybuilding.expection.SystemException;
import com.partybuilding.mapper.PartytableMapper;
import com.partybuilding.mapper.UsertableMapper;
import com.partybuilding.service.UsertableService;
import com.partybuilding.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author wulala
 */
@Service
public class UsertableServiceImpl extends ServiceImpl<UsertableMapper, Usertable> implements UsertableService {

    @Autowired
    private PartytableMapper partytableMapper;
    @Override
    public ResponseResult writePartyTime() {
        //根据id写入入党时间

//        用于封装查询条件
        LambdaQueryWrapper<Usertable> queryWrapper=new LambdaQueryWrapper<>();
        //id等于传入的id
        queryWrapper.eq(Usertable::getId,1);

        return null;
    }

    @Override
    @DataScope
    public ResponseResult getAllUser(LambdaQueryWrapper<Usertable> queryWrapper) {
        //根据id排序
        queryWrapper.orderByAsc(Usertable::getId);
        //只显示预备党员+正式党员
        queryWrapper.and((wrapper)->{
            wrapper.eq(Usertable::getPd_status,SystemConstants.POLITICAL_STATUS_PARTYMEMBER)
                    .or()
                    .eq(Usertable::getPd_status,SystemConstants.POLITICAL_STATUS_PREPARATIVE);
        });
        List<Usertable> usertables= list(queryWrapper);
        //返回id前10条
//        Page<Usertable> page=new Page(1,10);
//        page(page,queryWrapper);

        //Bean 拷贝并返回
        return ResponseResult.okResult(BeanCopyUtils.copyBeanList(usertables,UserCheckVo.class));
    }


    /**
     * @param id
     * @param queryWrapper,作为查询语句，通过切面函数后自带支部范围
     * @return
     */
    @DataScope//用于表示范围查询
    public ResponseResult getUser(Long id,LambdaQueryWrapper<Usertable> queryWrapper) {
        //根据id排序
        //只显示预备党员+正式党员
        queryWrapper
                .eq(Usertable::getId,id);
        List<Usertable> usertables= list(queryWrapper);

        //Bean 拷贝并返回
        return ResponseResult.okResult(BeanCopyUtils.copyBeanList(usertables,UserCheckVo.class));
    }



    /**
     * 传入List<Usertable>若库中有对应id，则覆写，若没有对应id，则创建并写入
     * 据用户id写入数据库，
     * @return
     */
    @Override

    public ResponseResult uploadAllData(List<UserCheckVo> userCheckVos) {

        ResponseResult result;
        LambdaQueryWrapper<Partytable> queryWrapper=new LambdaQueryWrapper<>();

        //删除缺少id的空对象
        userCheckVos.removeIf(userCheckVo ->  Objects.isNull(userCheckVo.getPd_id()));
        //将前端接收对象转换为entity对象
        List<Usertable> usertables=BeanCopyUtils.copyBeanList(userCheckVos,Usertable.class);
        List<Usertable> usertablesupdate=new ArrayList<Usertable>();

        // 创建一个SimpleDateFormat对象，用于格式化时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        /*
        TODO: 2024/6/24  可以添加其余业务处理逻辑
        根据partyID修改对应参数,如入党日期;
        */
        for (int i=0; i<userCheckVos.size();i++) {
            UserCheckVo userCheckVo=userCheckVos.get(i);
            Usertable usertable=usertables.get(i);
            queryWrapper
                    .eq(Partytable::getBranches_id,userCheckVo.getPd_branchesid());
            Partytable partytable= partytableMapper.selectOne(queryWrapper);
            // 获取系统当前时间
            Date date = new Date();
            //维护更新时间
            usertable.setGmt_modified(date);
            usertable.setGmt_create(date);
            usertable.setIs_delete(0);
            //将需要更新内容换至update文件中
//            usertablesupdate.set(i,usertable);
        }

//        UpdateWrapper<Usertable> updateWrapper = new UpdateWrapper<>();




//        if (saveOrUpdateBatch(usertables, usertables.size())){
        if (updateBatchById(usertables, usertables.size())){
            //如果返回1表示更新成功
            result=ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),AppHttpCodeEnum.SUCCESS.getMsg());
        }else{
            result=ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(),AppHttpCodeEnum.SYSTEM_ERROR.getMsg()+"更新错误");
        }
        return result;
    }

    @Override
    public ResponseResult upDateOneUser(Usertable usertable) {
        ResponseResult result;
        if(saveOrUpdate(usertable)) {
            result=ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),AppHttpCodeEnum.SUCCESS.getMsg());
        }
        else {
            throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        return result;
    }

    @Override
    public ResponseResult setOrGetKey(){
        ResponseResult result;
        result=ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),AppHttpCodeEnum.SUCCESS.getMsg());
        return result;
    }

}
